package com.elsa.smart_buffet.service.impl;

import com.elsa.smart_buffet.mapper.ConsumerMapper;
import com.elsa.smart_buffet.pojo.Comsumer;
import com.elsa.smart_buffet.pojo.ResultBox.CallBackMessage;
import com.elsa.smart_buffet.pojo.ResultBox.ResponseResult;
import com.elsa.smart_buffet.service.ConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@Transactional
public class ConsumerServiceImpl implements ConsumerService {

    @Autowired
    private ConsumerMapper consumerMapper;

    @Override
    public ResponseResult selectAllConsumer() {
        return new ResponseResult(CallBackMessage.SUCCESS, consumerMapper.selectList(null));
    }

    @Override
    public ResponseResult selectOneByIdConsumer(int id) {
        return new ResponseResult(CallBackMessage.SUCCESS, consumerMapper.selectById(id));
    }

    @Override
    public ResponseResult selectConsumerCount() {
        return new ResponseResult(CallBackMessage.SUCCESS, consumerMapper.selectCount(null));
    }

    @Override
    public ResponseResult updateConsumer(Comsumer comsumer) {
        return new ResponseResult(CallBackMessage.SUCCESS, consumerMapper.updateById(comsumer));
    }

    @Override
    public ResponseResult deleteByIdConsumer(int id) {
        return new ResponseResult(CallBackMessage.SUCCESS, consumerMapper.deleteById(id));
    }

    @Override
    public ResponseResult insertConsumer(Comsumer comsumer) {
        return new ResponseResult(CallBackMessage.SUCCESS, consumerMapper.insert(comsumer));
    }

    @Override
    public ResponseResult getConsumerByTimeAndSex(String data) {
        String[] info = data.split("\\-");
        for(int i = 0; i < info.length - 1; i++) {
            info[i] = info[i].replace("/", "-");
            log.info(info[i]);
        }
        return new ResponseResult(CallBackMessage.SUCCESS, consumerMapper.getConsumerByTimeAndSex(info[0], info[1], info[2]));
    }

    @Override
    public ResponseResult getConsumerBySearchData(String data) {
        return new ResponseResult(CallBackMessage.SUCCESS, consumerMapper.getConsumerBySearchData(data));
    }

}
